﻿//We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0. 
//Example: 3, -2, 1, 1, 8 -> 1+1-2=0.

using System;

class Program
{
    static void Main()
    {
        int numberCount = 5;
        int sum = 0;
        int[] numbersArray = new int[numberCount];
        for (int i = 1; i <= numberCount; i++)
        {
            Console.Write("Enter integer number ({0}): ", i);
            string numberString = Console.ReadLine();
            int number;
            while (!int.TryParse(numberString, out number))
            {
                Console.Write("Invalid input. Enter integer number ({0}) again: ", i);
                numberString = Console.ReadLine();
            }
            numbersArray[i - 1] = number;
        }
        int MaxBitNumber = (int)Math.Pow(2, numberCount) - 1;
        int sumCount = 0;
        //count from 00001 to 11111
        for (int i = 1; i <= MaxBitNumber; i++)
        {
            int currentSum = 0;
            for (int j = 0; j < numberCount; j++)
            {
                //get bit at position j in number i
                int mask = 1 << j;
                int nAndMask = i & mask;
                int bit = nAndMask >> j;
                if (bit == 1)
                {
                    currentSum = currentSum + numbersArray[j];
                }
            }
            if (currentSum == sum)
            {
                sumCount++;
            }
        }
        Console.WriteLine("Found {0} subset with sum = {1}", sumCount, sum);
    }
}
